/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.ChargeCheckItemBean;
import com.naizhuai.ui.entity.VirtualChosenBean;
import com.naizhuai.util.ImageUtils;

import java.util.List;

public class ThankChosenUserAdapterHolder extends RecyclerView.Adapter {

    public final static int TYPE_CLICK_DETAILS = 1;
    public final static int TYPE_CLICK_ADD = 2;

    private OnItemClickListener mItemClickListener;
    private BaseActivity mContext;
    private List<PersonUserBean> virBeans;


    public ThankChosenUserAdapterHolder(BaseActivity context,List<PersonUserBean> virBeans) {
        this.mContext = context;
        this.virBeans = virBeans;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < virBeans.size())
            return TYPE_CLICK_DETAILS;
        else
            return TYPE_CLICK_ADD;
    }

    @Override
    public int getItemCount() {
        return virBeans.size()+1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_CLICK_DETAILS) {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_thank_user_item, parent, false);
            return new PersonItemHolder(sView);
        } else {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_thank_add_item, parent, false);
            return new AddItemHolder(sView,TYPE_CLICK_ADD);
        }
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PersonItemHolder) {
            PersonItemHolder personHolder = (PersonItemHolder) holder;
            ImageUtils.glideThumbViewByUrl(mContext, personHolder.userThumb, virBeans.get(position).getUserLogoUrl());
            personHolder.userName.setText(virBeans.get(position).getNickName());
        } else if (holder instanceof AddItemHolder) {
            AddItemHolder operateHolder = (AddItemHolder) holder;
            if(operateHolder.getType()==TYPE_CLICK_ADD)
                operateHolder.operateIcon.setImageResource(R.mipmap.thank_add_icon);
            else{
                operateHolder.operateIcon.setImageResource(R.mipmap.delete_user);
                if(virBeans.size()==0)
                    operateHolder.itemView.setVisibility(View.GONE);
                else
                    operateHolder.itemView.setVisibility(View.VISIBLE);
            }
        }
    }


    public class PersonItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        CircleImageView userThumb;
        TextView userName;
        ImageView delete;

        public PersonItemHolder(View view) {
            super(view);
            userThumb = (CircleImageView) view.findViewById(R.id.user_head_logo);
            userName = (TextView) view.findViewById(R.id.user_name);
            delete=(ImageView)view.findViewById(R.id.delete_fork);
            delete.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null)
                mItemClickListener.onItemClick(v, getAdapterPosition(), TYPE_CLICK_DETAILS);
        }
    }

    public class AddItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView operateIcon;
        private int type;

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public AddItemHolder(View view, int type) {
            super(view);
            this.type = type;
            operateIcon=view.findViewById(R.id.operate_icon);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null)
                mItemClickListener.onItemClick(v, getAdapterPosition(), type);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position, int clickType);
    }

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
